package com.fr.plugin.export.xml.exporter;

import com.fanruan.api.log.LogKit;
import com.fanruan.api.report.PrintKit;
import com.fanruan.api.report.export.BaseAppExporter;
import com.fanruan.api.util.GeneralKit;
import com.fanruan.api.util.StringKit;
import com.fr.base.Style;
import com.fr.main.workbook.ResultWorkBook;
import com.fr.page.ClippedPageProvider;
import com.fr.page.PageSetProvider;
import com.fr.page.PaperSettingProvider;
import com.fr.page.ReportPageProvider;
import com.fr.plugin.context.PluginContexts;
import com.fr.report.cell.CellElement;
import com.fr.report.elementcase.ElementGetter;
import com.fr.report.report.ECReport;
import com.fr.report.report.ResultReport;
import com.fr.stable.EncodeConstants;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.Format;
import java.util.Iterator;
import java.util.List;

/**
 * Created by richie on 16/1/21.
 */
public class XmlExporter extends BaseAppExporter {

    @Override
    public void export(OutputStream out, ResultWorkBook book) throws Exception {
        List paperSettingList = PrintKit.getPaperSettings(book);
        for (int i = 0, len = book.getReportCount(); i < len; i++) {
            this.export(out, book.getResultReport(i), (PaperSettingProvider) paperSettingList.get(i));
        }
    }

    @Override
    public void export(java.io.OutputStream out, PageSetProvider pageSet) throws Exception {
        for (int i = 0; i < pageSet.size(); i++) {
            ReportPageProvider reportPage = pageSet.getPage(i);
            ClippedPageProvider page = PrintKit.pick(reportPage);
            if (page == null) {
                break;
            }
            this.exportReport(out, (ElementGetter) page, 0, (page).getRowCount());
        }
    }

    private void export(OutputStream out, ResultReport report, PaperSettingProvider paperSetting)
            throws Exception {
        if (report != null) {
            LogKit.info("UnLayerReport start export");
            exportReport(out, (ECReport) report, 0, (report).getRowCount());
        }
    }

    public void exportReport(OutputStream out, ElementGetter reportCase, int start, int end) throws Exception {
        if (PluginContexts.currentContext().isAvailable()) {
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, EncodeConstants.ENCODING_UTF_8));
            StringBuilder xmlBuffer = new StringBuilder();
            xmlBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
            xmlBuffer.append("<report>");
            for (int row = start; row < end; row++) {
                xmlBuffer.append("<row>");
                Iterator it = reportCase.getRow(row);

                while (it.hasNext()) {
                    xmlBuffer.append("<col>");
                    String exportText;
                    CellElement cell = (CellElement) it.next();
                    if (cell == null) {
                        exportText = StringKit.EMPTY;
                    } else {
                        boolean export = PrintKit.isCellPrintable(cell);
                        if (export) {
                            Object value = cell.getValue();
                            Style style = cell.getStyle();
                            if (style != null) {
                                Format format = style.getFormat();
                                exportText = Style.valueToText(value, format);
                            } else {
                                exportText = GeneralKit.objectToString(value);
                            }
                        } else {
                            exportText = StringKit.EMPTY;
                        }
                    }
                    xmlBuffer.append(exportText);
                    xmlBuffer.append("</col>");

                }
                xmlBuffer.append("</row>");
            }
            xmlBuffer.append("</report>");
            writer.println(xmlBuffer.toString());
            writer.flush();
        } else {
            throw new RuntimeException("XML Export Plugin License Expired!");
        }
    }
}